package interfaz;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class DialogoUsarAplicacion extends JDialog implements ActionListener
{
	private JTextField[] txts;
	private JButton btnUsar;
	private JButton btnLimpiar;
	private String nombre;
	
	private InterfazP2P interfaz;
	
	public DialogoUsarAplicacion( InterfazP2P i, String nombre, String[] parametros )
	{
		interfaz = i;
		setLocation( (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2 );
		JPanel panel = new JPanel( new GridBagLayout( ) );
		setSize( new Dimension( 300,200 ));
		setResizable( false );
		setTitle( "Usar Aplicacion: " + nombre );
		panel.setSize( new Dimension( 500,250 ));
		this.nombre = nombre;
		
		
		txts = new JTextField[ parametros.length ];
		
		btnUsar = new JButton( "Usar Aplicacion" );
		btnUsar.addActionListener( this );
		btnUsar.setActionCommand( "USAR" );
		
		btnLimpiar = new JButton( "Limpiar Forma" );
		btnLimpiar.addActionListener( this );
		btnLimpiar.setActionCommand( "LIMPIAR" );
		
		GridBagConstraints c = new GridBagConstraints( );
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets( 5,5,5,5 );
		
		for( int k = 0; k < parametros.length; k++ )
		{
			c.gridx = 0;
			c.gridy = k;
			panel.add( new JLabel( parametros[k] ), c );
		}
		for( int k = 0; k < parametros.length; k++ )
		{
			c.gridx = 1;
			c.gridwidth = 3;
			c.weightx = 1;
			c.gridy = k;
			txts[k] = new JTextField( 10 );
			if( !parametros[0].equals("") )
				panel.add( txts[k], c );
		}
		
		c.gridwidth = 1;
		c.weightx = 0;
		c.gridx = 0;
		c.gridy = parametros.length;
		panel.add( btnUsar, c );
		
		c.gridx = 1;
		panel.add( btnLimpiar, c );
		
		
		add( panel );
	}
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String cmd = e.getActionCommand();
		if( cmd.equals("USAR") )
		{
			String[] params = new String[ txts.length ];
			for( int k = 0; k < txts.length; k++ )
				params[k] = txts[k].getText();
			interfaz.usarAplicacion( this, nombre, params );
		}
		else if( cmd.equals("ELIMINAR") )
		{
			for( int k = 0; k < txts.length; k++ )
				txts[k].setText( "" );
		}
	}
}
